package org.chandler.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.lang3.StringEscapeUtils;
import org.chandler.ccgp.data.Task;
import org.chandler.data.Bidding;
import org.chandler.util.SqlUtil;
import org.springframework.web.util.HtmlUtils;

public class BiddingDAO {
	
	/**
	 * 初始化任务时插入
	 * @param t
	 * @return
	 */
	public static int insert(Bidding bidding){
		int id = 0;
		Connection conn = null;
		try {
			conn = SqlUtil.GetConn();
			Statement stmt = conn.createStatement();
			stmt.execute(genInsertInitSql(bidding), Statement.RETURN_GENERATED_KEYS);
			ResultSet rs = stmt.getGeneratedKeys();
			if(rs.next()){  
			    id = rs.getInt(1);  
			}
		} catch (SQLException e) {
			SqlUtil.CloseConn(conn);
			e.printStackTrace();
		}
		SqlUtil.CloseConn(conn);
		return id;
	}
	
	private static String genInsertInitSql(Bidding b){
		StringBuffer sb = new StringBuffer();
		sb.append("INSERT INTO `bidding`(`inter_id`, `name`, `url`, `type`, `catagory`, "
				+ "`buyer`, `agent`, `location`, `html`, `summary`, `date_time`, `time`, `source`, `isFinish`) VALUES");
		sb.append("('");
		sb.append(b.interId + "','");
		sb.append(b.name + "','");
		sb.append(b.url + "','");
		sb.append(b.type + "','");
		sb.append(b.catagory + "','");
		sb.append(b.buyer + "','");
		sb.append(b.agent + "','");
		sb.append(b.location + "','");
		sb.append(HtmlUtils.htmlEscape(b.html) + "','");
		sb.append(HtmlUtils.htmlEscape(b.summary) + "','");
		sb.append(b.dateTime + "','");
		sb.append(b.time + "','");
		sb.append(b.source + "',");
		sb.append(1 + ")");
		return sb.toString();
	}
}
